package com.kklldog.tasks;

import java.util.List;

import com.kklldog.core.task.*;

import com.kklldog.po.User;
import com.kklldog.services.UserService;

/** 
 * @author mj.zhou 
 * @version create：2013-12-4 下午3:00:07 
 * Login异步调用类
 */
public class UserOperationTask extends  BaseAsyncTask {

	public final static String EXCUTE_LOGIN  ="excuteLogin";
	public final static String EXCUTE_GETALL  ="excuteGetAll";
	
	public TaskResult<User> excuteLogin(TaskParams param) {
		
		TaskResult<User> result = new TaskResult<User>();
		result.setIsSuccess(false);
		result.setCompleteMethodName(param.getExecMethodName());
		
		UserService service=new UserService();
		String userName = (String)param.get("userName");
		String psw =  (String)param.get("password");
		
	    try {
			User user =	service.login(userName, psw);
			if(user!=null){
				result.setData(user);
				result.setIsSuccess(true);
			}
			else{
				result.setIsSuccess(false);
				result.setErrorMessage("用户名或密码错误");
			}
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorMessage("网络异常");
		}
	    
	    return result;
	}

	public TaskResult<List<User>> excuteGetAll(TaskParams param)
	{
		UserService service=new UserService();
		TaskResult<List<User>> result = new TaskResult<List<User>>();
		result.setIsSuccess(false);
		result.setCompleteMethodName(param.getExecMethodName());
		try {
			List<User> users = service.getAll(User.class);
			result.setIsSuccess(true);
			result.setData(users);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	@Override
	protected String getClassName() {
		// TODO Auto-generated method stub
		return "com.kklldog.tasks.UserOperationTask";
	}


}
